package net.comm.param.bean;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import net.comm.db.DBProcException;
import net.comm.param.db.DbPropertyService;
import net.comm.param.db.PropertyServiceSqlMysql;
import net.comm.util.DbcpPool;

import org.apache.log4j.Logger;
import org.sakaiproject.db.impl.BasicSqlService;


public class Property {

	static Logger logger = Logger.getLogger(Property.class.getName());
	
	public Property() {
		super();
		// TODO Auto-generated constructor stub
		Assortmap = new HashMap<Integer, String>();
		Propmap = new HashMap<String, String>();
	}
	private  HashMap<Integer, String> Assortmap;
	private  HashMap<String, String> Propmap;
	
	private int  AssortID = 0;				//分类ID
	private String  Assortname = "";		//分类名称
	private int  PropID = 0;				//属性ID
	private String  Propname = "";			//属性名称
	private int  ParentID = 0;				//所属属性
	private int  Exichiflag = 0;			//是否有下一级标志

	//根据分类名称取对应的分类ID
	public int getAssortID(String assortname) {
		
		
		Connection cn = null;
		try
		{
			if(assortname == null)
			{
				return 0;
			}
			if(assortname.equals(""))
			{
				return 0;
			}

			//判断AssortIDmap中是否存在，存在则返回
			for (Iterator<Integer> i = Assortmap.keySet().iterator(); i.hasNext();) { 
				int key = i.next(); 
				String value = Assortmap.get(key); 
				if(value.equals(assortname)){
					AssortID = key;
					break;
				}
					
			} 			
			
			if(AssortID  > 0)
			{
				return AssortID;
			}

			cn = DbcpPool.getConnection();
			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[1];
			
			fields2[0] = assortname;
			
			
			PropertyServiceSqlMysql servSql = new PropertyServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getRecbyAssortNameSql();			
					
			List list = bSqlServ.dbRead(cn, sql, fields2, new DbPropertyService());
			if(list.size() == 0){

				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return 0;				
			}
			BaseProperty base = (BaseProperty)list.get(0);
			
			AssortID = base.getAssortID();
			
			Assortmap.put(AssortID, assortname);
			
			DbcpPool.returnConnection(cn);
			return AssortID;
			
		} catch(DBProcException ex) 
		{
			logger.info(ex.getMessage());
			System.out.println(ex.getMessage());
		} catch(Exception ex) 
		{
			logger.info(ex.getMessage());
			System.out.println(ex.getMessage());
		} finally {
			DbcpPool.returnConnection(cn);
		}	
		
		return AssortID;
	}
	//根据分类名称取对应的分类ID
	public String getPropname(int assortID, int propID) {
		
		
		Connection cn = null;
		try
		{
			if(propID == 0||assortID == 0)
			{
				return "";
			}

			//判断hashmap中是否存在，存在则返回
			Propname = Propmap.get(String.valueOf(assortID) + String.valueOf(propID));
			if(Propname != null && Propname.trim().length() > 0)
			{
				return Propname;
			}		

			cn = DbcpPool.getConnection();

			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[2];
			
			fields2[0] = assortID;
			fields2[1] = propID;
			
			
			PropertyServiceSqlMysql servSql = new PropertyServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getbykeySql();			
					
			List list = bSqlServ.dbRead(cn, sql, fields2, new DbPropertyService());
			
			if(list.size() == 0){

				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return "";				
			}
			BaseProperty base = (BaseProperty)list.get(0);
				
			Propmap.put(String.valueOf(assortID) + String.valueOf(propID), base.getPropname());
			Propname = base.getPropname();
				
			DbcpPool.returnConnection(cn);
			return Propname;
			
		} catch(DBProcException ex) 
		{
			logger.info(ex.getMessage());
		} catch(Exception ex) 
		{
			logger.info(ex.getMessage());
		} finally {
			DbcpPool.returnConnection(cn);
		}	
		
		return Propname;
	}	
	
	public void setAssortID(int assortID) {
		AssortID = assortID;
	}
	public String getAssortname(int assortID) {
		
		
		Connection cn = null;
		try
		{
			if(assortID == 0)
			{
				return "";
			}
			//判断hashmap中是否存在，存在则返回
			Assortname = Assortmap.get(assortID);
			if(Assortname != null && Assortname.trim().length() > 0)
			{
				return Assortname;
			}
			
			cn = DbcpPool.getConnection();

			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[1];
			
			fields2[0] = assortID;
			
			
			PropertyServiceSqlMysql servSql = new PropertyServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getRecbyAssortIDSql();			
					
			List list = bSqlServ.dbRead(cn, sql, fields2, new DbPropertyService());
			
			if(list.size() == 0){

				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return "";				
			}
			BaseProperty base = (BaseProperty)list.get(0);			

			Assortname = base.getAssortname();
			
			Assortmap.put(assortID, Assortname);
			
			DbcpPool.returnConnection(cn);
			return Assortname;
			
		} catch(DBProcException ex) 
		{
			logger.info(ex.getMessage());
		} catch(Exception ex) 
		{
			logger.info(ex.getMessage());
		} finally {
			DbcpPool.returnConnection(cn);
		}	
		return Assortname;
	}
	public HashMap<Integer, String> getAssortmap() {
		return Assortmap;
	}
	public void setAssortmap(HashMap<Integer, String> assortmap) {
		Assortmap = assortmap;
	}
	public HashMap<String, String> getPropmap() {
		return Propmap;
	}
	public void setPropmap(HashMap<String, String> propmap) {
		Propmap = propmap;
	}
	public int getPropID() {
		return PropID;
	}
	public void setPropID(int propID) {
		PropID = propID;
	}
	public String getPropname() {
		return Propname;
	}
	public void setPropname(String propname) {
		Propname = propname;
	}
	public int getParentID() {
		return ParentID;
	}
	public void setParentID(int parentID) {
		ParentID = parentID;
	}
	public int getAssortID() {
		return AssortID;
	}
	public String getAssortname() {
		return Assortname;
	}
	public void setAssortname(String assortname) {
		Assortname = assortname;
	}


	
}
